import { _decorator, Component, Node, Vec3, UITransform, Label, sp } from 'cc';
import { App } from '../../../core/app';
import { ToolsHelper } from '../../../utils/toolsHelper';
import { Constant } from '../../../const/enumConst';
const { ccclass, property } = _decorator;

@ccclass('gridCmpt')
export class gridCmpt extends Component {
    /** 横纵轴编号 */
    public h: number = 0;
    public v: number = 0;
    public type: number = -1;
    public obstacleValue: number = 0;
    public data: { h: number, v: number }

    // 新增：冰块层数属性
    public iceLayerCount: number = 0;

    public initData(h: number, v: number, type: number = -1, iceLayers: number = 0) {
        this.h = h;
        this.v = v;
        this.data = { h: h, v: v }
        if (type > -1) {
            this.type = type;
        }
        if (this.type == -1) {
            this.type = Math.floor(Math.random() * App.gameLogic.blockCount);
            // this.type = Math.floor(Math.random() * 2);
        }



        this.node.getChildByName('icon').children.forEach(item => {
            item.active = false;
            if (item.name == `Match${this.type}`) {
                item.active = true;
            }
            if (item.name == this.type + "") {
                item.active = true;
            }
            if (item.active && item.getChildByName('f')) {
                item.getChildByName('f').active = false;
            }
        });
        // 新增：设置冰块层数
        this.setIceLayerCount(iceLayers);
        // this.node.getChildByName('yuansuk').active = true;

        // this.showPos(h, v);
    }

    // 新增：设置冰块层数
    setIceLayerCount(count: number) {
        // console.log("冰块层数",count)
        this.iceLayerCount = count;
        this.updateIceLayerDisplay();
    }

    // 新增：减少冰块层数
    reduceIceLayer(): boolean {
        if (this.iceLayerCount > 0) {
            this.iceLayerCount--;
            this.updateIceLayerDisplay();
            // console.log(`冰块层数减少: ${this.h},${this.v} -> ${this.iceLayerCount}`);
            return true;
        }
        return false;
    }

    // 新增：检查是否有冰块
    hasIce(): boolean {
        return this.iceLayerCount > 0;
    }

    // 新增：更新冰块层数显示
    updateIceLayerDisplay() {
        const iceNode = this.node.getChildByName("icon").getChildByName("201");
        if (this.iceLayerCount > 0) {
            // 显示冰块层数
            this.setCount(this.iceLayerCount);
            // 确保冰块节点可见
            iceNode.active = true;
        } else if (this.type > Constant.NormalType) {
            // 障碍物且无冰块层数时隐藏
            iceNode.active = false;
        }
    }
    onDisable() {
        this.type = -1;
    }

    // showPos(h: number = this.h, v: number = this.v) {
    //     let lb = this.node.getChildByName('lb');
    //     // lb.getComponent(Label).string = this.type+'';
    // }

    isInside(pos: Vec3): boolean {
        let width = this.node.getComponent(UITransform).width;
        let curPos = this.node.position;
        if (Math.abs(pos.x - curPos.x) <= width / 2 && Math.abs(pos.y - curPos.y) <= width / 2) return true;
        return false;
    }

    /** 选中状态 */
    setSelected(bool: boolean) {
        if (!this.isValid) return;
        this.node.getChildByName('icon').children.forEach(item => {
            if (item.active && item.getChildByName('s')) {
                item.getChildByName('s').active = bool;
            }
        })
    }

    getMoveState() {
        return false;
    }

    setFly() {
        // console.log('飞！！！！！')
        if (!this.isValid) return;
        this.node.getChildByName('icon').children.forEach(item => {
            if (item.active && item.getChildByName('f')) {
                // console.log('飞展示！！！！！')

                item.getChildByName('f').active = true;
                // console.log(item.getChildByName('f'))

                item.getChildByName('f').getComponent(sp.Skeleton).setAnimation(0, 'move', true);
            }
        })
    }

    setType(type: number) {
        if (!this.isValid) return;
        this.type = type;
        this.node.getChildByName('icon').children.forEach(item => {
            item.active = false;
            if (item.name == `Match${this.type}`) {
                item.active = true;
            }
            if (item.name == this.type + "") {
                item.active = true;
            }
            if (item.active && item.getChildByName('f')) {
                item.getChildByName('f').active = false;
            }
        });
        // this.showPos()
    }
    setCount(count: number) {
        console.log("当前冰块",count)
        let lb = this.node.getChildByName('icon').getChildByName('201').getChildByName('lb');
        lb.getComponent(Label).string = `${count}`;
        lb.active = true;
        if (count == 0) {
            // this.node.getChildByName('ok').active = true;
            lb.active = false;
        }
    }
    showGou(bool: boolean) {
        this.node.getChildByName('gou').active = bool;
    }

    /** 显示提示 */
    async showTips() {
        this.node.getChildByName("selected").active = true;
        await ToolsHelper.delayTime(2);
        if (this.isValid) {
            this.node.getChildByName("selected").active = false;
        }
    }
}